package com.example.capsule_chat0322.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.SignBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.request.SignRequest;
import com.example.capsule_chat0322.utils.AlterUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author zhuzhiming
 * @Date 2022/5/20 0:33
 */
public class SignActivity extends AppCompatActivity {

    private EditText account;
    private EditText username;
    private TextView sender_name;
    private Button add_commit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in);
        initView();
    }


    private void initView() {
        account = findViewById(R.id.account);
        username = findViewById(R.id.username);
        add_commit = findViewById(R.id.add_commit);
        sender_name = findViewById(R.id.sender_name);
        sender_name.setText("打卡签到");
        SignBean signBean = (SignBean) GlobalAssistMap.getObject("signBean");
        GlobalAssistMap.removeObject("signBean");
        String roomName = getIntent().getStringExtra("roomName");
        add_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String signAccount = account.getText().toString();
                String signUsername = username.getText().toString();

                if (signAccount.length() == 0 || signUsername.length() == 0) {
                    AlterUtils.alertByToastInMainThread(SignActivity.this, "请输入合法数据");
                    return;
                }

                doSign(roomName, signBean);
            }
        });

    }

    private void doSign(String roomName, SignBean signBean) {

        int signStatus = signBean.getSignStatus();

        if (signStatus == 1) {
            DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    sign(roomName, signBean);
                }
            };

            DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            };

            AlterUtils.alertByDialog(SignActivity.this, "提示", "你已经签到,再次签到会覆盖之前的,确定吗?", "确定", positiveListener, "取消", cancelListener);
        } else {
            sign(roomName, signBean);
        }


    }

    private void sign(String roomName, SignBean signBean) {

        int signStatus = signBean.getSignStatus();
        signBean.setAccount(account.getText().toString());
        signBean.setUserName(username.getText().toString());
        @SuppressLint("SimpleDateFormat") String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        signBean.setSignTime(time);
        if (signStatus == 0) {
            signBean.setRoomName(roomName);
            signBean.setField(GlobalAssistMap.getObject("field").toString());
            signBean.setSenderId(GlobalAssistMap.getObject("userId").toString());
        }

        SignRequest.doSign(signBean);
        AlterUtils.alertByToastInMainThread(SignActivity.this, "签到成功");
        finish();
    }


}
